package com.java01.apache.commons.exec;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;

public class ExecutionStreamHandler {

	public static void main(String[] args) {
		DefaultExecutor executor = new DefaultExecutor();

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		PumpStreamHandler streamHandler = new PumpStreamHandler(baos);
		executor.setStreamHandler(streamHandler);

		CommandLine commandLine = CommandLine.parse("ipconfig");

		try {
			int exitCode = executor.execute(commandLine);
			System.out.println("exit "+exitCode);
		} catch (ExecuteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println(new String(baos.toByteArray()));
	}
}
